﻿using SlamDunk.CMS.DAL;
using SlamDunk.CMS.Model;
using System;
using System.Collections.Generic;
using SlamDunk.CMS.Model.DataModel;

namespace SlamDunk.CMS.BLL
{
    public class ArticleBll
    {
        #region 01.实例化数据层
        /// <summary>
        /// 实例化数据层
        /// </summary>
        private readonly ArticleDal CurrentDal = new ArticleDal();
        #endregion

        #region 02.添加
        /// <summary>
        /// 添加一个实体对象
        /// </summary>
        /// <param name="article">构造好要添加的实体对象</param>
        /// <returns>添加完毕后的实体对象</returns>
        public Article Add(Article article)
        {
            return CurrentDal.Add(article);
        }
        #endregion

        #region 03.删除（物理删除）
        /// <summary>
        /// 删除一个实体对象
        /// </summary>
        /// <param name="id">要删除的实体对象Id</param>
        /// <returns>删除结果</returns>
        public bool PhysicalDelete(int id)
        {
            return CurrentDal.PhysicalDelete(id) > 0;
        }
        #endregion

        #region 05.修改
        /// <summary>
        /// 修改一个实体对象
        /// </summary>
        /// <param name="article">构造好的要修改的实体对象</param>
        /// <returns>修改结果</returns>
        public bool Update(Article article)
        {
            return CurrentDal.Update(article) > 0;
        }
        #endregion

        #region 06.根据主键获取单个实体对象
        /// <summary>
        /// 根据主键获取单个实体对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>单个实体对象</returns>
        public Article GetModel(int id)
        {
            return CurrentDal.GetModel(id);
        }
        #endregion

        #region 07.获取实体对象集合
        /// <summary>
        /// 获取所有实体对象
        /// </summary>
        /// <returns>实体对象集合</returns>
        public List<Article> GetModelList()
        {
            return CurrentDal.GetModelList();
        }
        #endregion

        #region 08.获取总记录条数
        /// <summary>
        /// 返回总记录条数
        /// </summary>
        /// <returns>总记录条数</returns>
        public int GetCount()
        {
            return CurrentDal.GetCount();
        }
        #endregion

        #region 01.根据条件获取记录条数
        /// <summary>
        /// 根据条件获取记录条数
        /// </summary>
        /// <param name="arg1">条件参数</param>
        /// <param name="arg2">条件参数</param>
        /// <param name="arg3">条件参数</param>
        /// <param name="arg4">条件参数</param>
        /// <returns>记录条数</returns>
        public int GetCount(string arg1, string arg2, string arg3, string arg4)
        {
            //1个参数
            if (!string.IsNullOrWhiteSpace(arg1) && string.IsNullOrWhiteSpace(arg2) && string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                return this.CurrentDal.GetCount("Field1", arg1);
            }

            //2个参数
            if (!string.IsNullOrWhiteSpace(arg1) && !string.IsNullOrWhiteSpace(arg2) && string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                return this.CurrentDal.GetCount("Field1", arg1, "Field2", arg2);
            }

            //3个参数
            if (!string.IsNullOrWhiteSpace(arg1) && !string.IsNullOrWhiteSpace(arg2) && !string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                return this.CurrentDal.GetCount("Field1", arg1, "Field2", arg2, "Field3", arg3);
            }

            //4个参数
            return this.CurrentDal.GetCount("Field1", arg1, "Field2", arg2, "Field3", arg3, "Field4", arg4);
        }
        #endregion

        #region 02.根据条件分页获取实体对象集合
        /// <summary>
        /// 根据条件分页获取实体对象集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录条数</param>
        /// <param name="arg1">条件参数</param>
        /// <param name="arg2">条件参数</param>
        /// <param name="arg3">条件参数</param>
        /// <param name="arg4">条件参数</param>
        /// <returns>实体对象集合</returns>
        public List<Article> GetModelList(int pageIndex, int pageSize, out int pageCount, out int rowCount, string arg1, string arg2, string arg3, string arg4)
        {
            //1个参数
            if (!string.IsNullOrWhiteSpace(arg1) && string.IsNullOrWhiteSpace(arg2) && string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                //1.计算总记录条数与总页数
                rowCount = this.GetCount(arg1, null, null, null);
                pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
                //2.页索引处理
                pageIndex = pageIndex < 1 ? 1 : pageIndex;
                pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
                //3.计算起始行与终止行
                int start = (pageIndex - 1) * pageSize + 1;
                int end = pageIndex * pageSize;
                //4.调用数据层返回相应实体集合
                return this.CurrentDal.GetModelList(start, end, "Field1", arg1);
            }

            //2个参数
            if (!string.IsNullOrWhiteSpace(arg1) && !string.IsNullOrWhiteSpace(arg2) && string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                //1.计算总记录条数与总页数
                rowCount = this.GetCount(arg1, arg2, null, null);
                pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
                //2.页索引处理
                pageIndex = pageIndex < 1 ? 1 : pageIndex;
                pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
                //3.计算起始行与终止行
                int start = (pageIndex - 1) * pageSize + 1;
                int end = pageIndex * pageSize;
                //4.调用数据层返回相应实体集合
                return this.CurrentDal.GetModelList(start, end, "[Title]", arg1, "[Content]", arg2);
            }

            //3个参数
            if (!string.IsNullOrWhiteSpace(arg1) && !string.IsNullOrWhiteSpace(arg2) && !string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                //1.计算总记录条数与总页数
                rowCount = this.GetCount(arg1, arg2, arg3, null);
                pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
                //2.页索引处理
                pageIndex = pageIndex < 1 ? 1 : pageIndex;
                pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
                //3.计算起始行与终止行
                int start = (pageIndex - 1) * pageSize + 1;
                int end = pageIndex * pageSize;
                //4.调用数据层返回相应实体集合
                return this.CurrentDal.GetModelList(start, end, "Field1", arg1, "Field2", arg2, "Field3", arg3);
            }

           //4个参数
            else
            {
                //1.计算总记录条数与总页数
                rowCount = this.GetCount(arg1, arg2, arg3, arg4);
                pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
                //2.页索引处理
                pageIndex = pageIndex < 1 ? 1 : pageIndex;
                pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
                //3.计算起始行与终止行
                int start = (pageIndex - 1) * pageSize + 1;
                int end = pageIndex * pageSize;
                //4.调用数据层返回相应实体集合
                return this.CurrentDal.GetModelList(start, end, "Field1", arg1, "Field2", arg2, "Field3", arg3, "Field4", arg4);
            }
        }
        #endregion

        #region 03.获取上一篇文章对象
        /// <summary>
        /// 获取上一篇文章对象
        /// </summary>
        /// <param name="id">文章Id</param>
        /// <param name="channelId">栏目Id</param>
        /// <returns>文章实体对象</returns>
        public Article GetPrevModel(int id, int channelId)
        {
            return this.CurrentDal.GetPrevModel(id, channelId);
        }
        #endregion

        #region 04.获取下一篇文章对象
        /// <summary>
        /// 获取下一篇文章对象
        /// </summary>
        /// <param name="id">文章Id</param>
        /// <param name="channelId">栏目Id</param>
        /// <returns>下一篇文章对象</returns>
        public Article GetNextModel(int id, int channelId)
        {
            return this.CurrentDal.GetNextModel(id, channelId);
        }
        #endregion

        #region 07.根据栏目Id、开始时间、结束时间与标题关键字查询文章记录条数
        /// <summary>
        /// 根据栏目Id、开始时间、结束时间与标题关键字查询文章记录条数
        /// </summary>
        /// <param name="channelId">栏目Id</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="title">标题关键字</param>
        /// <returns>符合条件的文章记录条数</returns>
        public int GetCount(int channelId, string startTime, string endTime, string title)
        {
            return this.CurrentDal.GetCount(channelId, startTime, endTime, title);
        }
        #endregion

        #region 08.根据栏目Id、开始时间、结束时间与标题关键字查询文章列表
        /// <summary>
        /// 根据栏目Id、开始时间、结束时间与标题关键字查询文章列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageCount">符合条件的页数</param>
        /// <param name="rowCount">符合条件的记录条数</param>
        /// <param name="channelId">栏目Id</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="title">标题关键字</param>
        /// <returns>文章列表</returns>
        public List<Article> GetModelList(int pageIndex, int pageSize, out int pageCount, out int rowCount, int channelId, string startTime, string endTime, string title)
        {
            //1.计算总记录条数与总页数
            rowCount = this.GetCount(channelId, startTime, endTime, title);
            pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
            //2.页索引处理
            pageIndex = pageIndex < 1 ? 1 : pageIndex;
            pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
            //3.计算起始行与终止行
            int start = (pageIndex - 1) * pageSize + 1;
            int end = pageIndex * pageSize;
            //4.调用数据层返回相应实体集合
            return this.CurrentDal.GetModelList(start, end, channelId, startTime, endTime, title);
        }
        #endregion

        #region 09.根据栏目Id获取文章记录条数
        /// <summary>
        /// 根据栏目Id获取文章记录条数
        /// </summary>
        /// <param name="channelId">栏目Id</param>
        /// <returns>符合条件的文章记录条数</returns>
        public int GetCount(int channelId)
        {
            return this.CurrentDal.GetCount(channelId);
        }
        #endregion

        #region 10.根据栏目Id分页查询文章列表
        /// <summary>
        /// 根据栏目Id分页查询文章列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">符合条件的记录条数</param>
        /// <param name="channelId">栏目Id</param>
        /// <returns>文章列表</returns>
        public List<Article> GetModelList(int pageIndex, int pageSize, out int rowCount, int channelId)
        {
            //1.计算总记录条数
            rowCount = this.GetCount(channelId);
            int pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
            //2.页索引处理
            pageIndex = pageIndex < 1 ? 1 : pageIndex;
            pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
            //3.计算起始行与终止行
            int start = (pageIndex - 1) * pageSize + 1;
            int end = pageIndex * pageSize;
            return this.CurrentDal.GetModelList(start, end, channelId);
        }
        #endregion

        #region 11.根据栏目Id查询文章列表
        /// <summary>
        /// 根据栏目Id分页查询文章列表
        /// </summary>
        /// <param name="channelId">栏目Id</param>
        /// <returns>文章列表</returns>
        public List<Article> GetModelList(int channelId)
        {
            return this.CurrentDal.GetModelList(channelId);
        }
        #endregion

        #region 12.根据栏目Id与数量查询文章列表
        /// <summary>
        /// 根据栏目Id与数量查询文章列表
        /// </summary>
        /// <param name="channelId">栏目Id</param>
        /// <param name="count">数量</param>
        /// <returns>文章列表</returns>
        public List<Article> GetModelList(int channelId, int count)
        {
            return this.CurrentDal.GetModelListByCount(channelId, count);
        }
        #endregion

        #region 13.根据栏目Id起始行与终止行获取文章列表
        /// <summary>
        /// 根据栏目Id起始行与终止行获取文章列表
        /// </summary>
        /// <param name="channelId">栏目Id</param>
        /// <param name="start">起始行</param>
        /// <param name="end">终止行</param>
        /// <returns></returns>
        public List<Article> GetModelList(int channelId, int start, int end)
        {
            return this.CurrentDal.GetModelList(start, end, channelId);
        }
        #endregion
    }
}
